package com.ljw.gulimall.coupon.service.impl;

import com.ljw.common.to.MemberPrice;
import com.ljw.common.to.SkuReductionTo;
import com.ljw.gulimall.coupon.entity.MemberPriceEntity;
import com.ljw.gulimall.coupon.entity.SkuLadderEntity;
import com.ljw.gulimall.coupon.service.MemberPriceService;
import com.ljw.gulimall.coupon.service.SkuLadderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljw.common.utils.PageUtils;
import com.ljw.common.utils.Query;

import com.ljw.gulimall.coupon.dao.SkuFullReductionDao;
import com.ljw.gulimall.coupon.entity.SkuFullReductionEntity;
import com.ljw.gulimall.coupon.service.SkuFullReductionService;
import org.springframework.transaction.annotation.Transactional;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    private MemberPriceService memberPriceService;

    @Autowired
    private SkuLadderService skuLadderService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存sku的折扣、优惠、会员价格信息
     * @param skuReductionTos
     */
    @Override
    @Transactional
    public void saveSkuReductionTos(List<SkuReductionTo> skuReductionTos) {
        List<SkuLadderEntity> skuLadderEntities = new ArrayList<>();
        List<SkuFullReductionEntity> skuFullReductionEntities = new ArrayList<>();
        List<MemberPriceEntity> memberPriceEntities= new ArrayList<>();
        for (SkuReductionTo skuReductionTo : skuReductionTos) {
            //保存sku折扣优惠信息
            //若fullCount为0,则表明添加商品时为设置买满几件打多少折信息，此数据需要过滤调
            if (skuReductionTo.getFullCount() > 0){
                SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
                skuLadderEntity.setSkuId(skuReductionTo.getSkuId());
                skuLadderEntity.setFullCount(skuReductionTo.getFullCount());
                skuLadderEntity.setDiscount(skuReductionTo.getDiscount());
                skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());
                skuLadderEntities.add(skuLadderEntity);
            }

            //保存sku满减优惠信息
            //若买满价格若为0,则表明添加数据时未设置买满折扣信息，此数据需要过滤掉
            if (skuReductionTo.getFullPrice().compareTo(new BigDecimal("0")) == 1){
                SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
                skuFullReductionEntity.setSkuId(skuReductionTo.getSkuId());
                skuFullReductionEntity.setFullPrice(skuReductionTo.getFullPrice());
                skuFullReductionEntity.setReducePrice(skuReductionTo.getReducePrice());
                skuFullReductionEntity.setAddOther(skuReductionTo.getPriceStatus());
                skuFullReductionEntities.add(skuFullReductionEntity);
            }

            //保存sku会员价格信息
            List<MemberPrice> memberPrice = skuReductionTo.getMemberPrice();
            for (MemberPrice price : memberPrice) {
                //若会员价格为0则表明添加商品时未设置会员价格，此数据需要过滤
                if (price.getPrice().compareTo(new BigDecimal("0")) == 1){
                    MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                    memberPriceEntity.setSkuId(skuReductionTo.getSkuId());
                    memberPriceEntity.setMemberPrice(price.getPrice());
                    memberPriceEntity.setMemberLevelId(price.getId());
                    memberPriceEntity.setMemberLevelName(price.getName());
                    memberPriceEntities.add(memberPriceEntity);
                }
            }
        }

        this.saveBatch(skuFullReductionEntities);

        memberPriceService.saveBatch(memberPriceEntities);

        skuLadderService.saveBatch(skuLadderEntities);
    }

}